package com.hqyj.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.server.domain.Roles;
import com.hqyj.server.domain.Users;
import com.hqyj.server.domain.UsersRoles;
import com.hqyj.server.mapper.UsersMapper;
import com.hqyj.server.service.RolesService;
import com.hqyj.server.service.UsersRolesService;
import com.hqyj.server.service.UsersService;
import com.hqyj.server.util.CryptoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Resource
    private UsersRolesService usersRolesService;

    @Resource
    private RolesService rolesService;

    @Override
    public boolean reg(Users u) throws Exception {
        //凡是用户注册必须给用户分配一个默认的角色。必须的。
        //Roles r = rolesService.getById(1); //guest角色；

        //对用户密码进行加密。
        u.setPassword(CryptoUtil.md5BySalt(u.getPassword()));

        if (super.save(u)) {
            //获得用户的ID；
            //UsersRoles usersRoles = new UsersRoles();
            //usersRoles.setUid(u.getUid());
            //usersRoles.setRid(r.getRid());
            /*
            if(usersRolesService.save(usersRoles)){
                return true;
            }*/
            return false;
        }
        return super.save(u);
    }

    @Override
    public Users login(String username, String password) throws Exception{
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",CryptoUtil.md5BySalt(password));

        return super.getOne(queryWrapper);
    }

    @Override
    public Users adminLogin(String username, String password) throws Exception {
        //先用最笨的办法。
        //分两步走。
        //1.判断用户名和密码是否正确，说明有这个用户。
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",CryptoUtil.md5BySalt(password));
        Users loginUser = super.getOne(queryWrapper);
        if(loginUser!=null){
           //2.是不是管理员。
           List<Roles> rolesList=  usersRolesService.queryUsersRolesByUid(loginUser.getUid());
           List<String> roles = new ArrayList<>();
           for(Roles r : rolesList){
               roles.add(r.getRname());
           }

           loginUser.setRoles(roles);

           return loginUser;
        }

        return null;
    }

    @Override
    public List<String> queryRolesListByUid(Integer uid) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        queryWrapper.eq("uid",uid);

        Users loginUser = super.getOne(queryWrapper);
        if(loginUser!=null){
            //2.是不是管理员。
            List<Roles> rolesList=  usersRolesService.queryUsersRolesByUid(loginUser.getUid());
            List<String> roles = new ArrayList<>();
            for(Roles r : rolesList){
                roles.add(r.getRname());
            }

            loginUser.setRoles(roles);

            return roles;
        }
        return null;
    }
}
